package java课题设计.功能类;

import java课题设计.工具类.Seek;
 
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileOutputStream;
import java.io.IOException;
//该类用于绘制注册事宜以及书写关于注册方面的代码逻辑
public class Register extends JDialog implements DocumentListener, MouseListener {
    String word;
    String name;
    JLabel reghter =new JLabel(new ImageIcon("D:\\\\aaa\\\\登录注册界面\\\\注册.png"));
    public Register(JFrame parent){
        super(parent, "注册新账号", true); // 设置为模态对话框
        extracted();
        image();
 
 
        this.setVisible(true);
    }
    public void extracted(){
        this.setSize(488,430);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
 
        //reghter.addMouseListener(this);
    }
    public void image(){
        //添加用户名提示输入框
        JLabel usernameText = new JLabel(new ImageIcon("D:\\\\aaa\\\\登录注册界面\\\\用户名.png"));
        usernameText.setBounds(116,135,47,17);
        this.getContentPane().add(usernameText);
        //添加密码输入提示框
        JLabel passwordText = new JLabel(new ImageIcon("D:\\\\aaa\\\\登录注册界面\\\\密码.png"));
        passwordText.setBounds(130,195,32,16);
        this.getContentPane().add(passwordText);
        //添加用户名输入框
        JTextField username=new JTextField();
        username.setBounds(195,134,200,30);
        this.getContentPane().add(username);
        //获取到用户输入的用户名
        username.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                name=username.getText();
            }
        });
 
 
 
 
        //添加密码输入框
        JTextField password=new JTextField();
        password.setBounds(195,195,200,30);
        this.getContentPane().add(password);
        //添加监听器获取用户输入的密码
        password.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                word = password.getText();
            }
 
            @Override
            public void removeUpdate(DocumentEvent e) {
                word = password.getText();
            }
 
            @Override
            public void changedUpdate(DocumentEvent e) {
                word = password.getText();
            }
        });
 
        //添加注册按钮
        reghter.setBounds(200,250,128,128);
        this.getContentPane().add(reghter);
        //为注册按钮添加点击监听器
        reghter.addMouseListener(this);
        System.out.println("添加监听器成功");
 
    }
    //将注册的用户名和账号写入登陆数据的文件中
    public void write() throws IOException {
        if(this.name==null||this.word==null){
            System.out.println("注册失败");
            //跳出弹窗提示账号或密码有误
            JDialog wrong=new JDialog(this,null,true);
            wrong.setBounds(550,300,300,200);
            wrong.setTitle("注册失败");
            JLabel w=new JLabel("用户名或账号不能为空");
            w.setFont(new Font("宋体",Font.BOLD,18));
            w.setBounds(100,100,100,100);
            wrong.add(w);
            wrong.setAlwaysOnTop(true);
            wrong.setVisible(true);
        }
        String str=this.name+"&"+this.word+"\n";
        //循环遍历文件判断是否有与注册的用户名相同的用户账号
        boolean flag= Seek.seek(str);
        if(flag==false){
            //添加注册失败弹窗
            System.out.println("注册失败");
            //跳出弹窗提示账号或密码有误
            JDialog wrong=new JDialog(this,null,true);
            wrong.setBounds(550,300,300,200);
            wrong.setTitle("注册失败");
            JLabel w=new JLabel("注册失败，用户名重复");
            w.setFont(new Font("宋体",Font.BOLD,25));
            w.setBounds(100,100,100,100);
            wrong.add(w);
            wrong.setAlwaysOnTop(true);
            wrong.setVisible(true);
        }
        else{
        FileOutputStream w=new FileOutputStream("D:\\aaa\\数据\\登陆数据.txt",true);
        byte[] byt=str.getBytes();
        w.write(byt);
        w.close();
        //添加注册成功提示窗口
        JDialog wrong=new JDialog(this,null,true);
        wrong.setBounds(550,300,300,200);
        wrong.setTitle("注册成功");
        JLabel wa =new JLabel("注册成功");
        wa.setFont(new Font("宋体",Font.BOLD,25));
        wa.setBounds(100,100,100,100);
        wrong.add(wa);
        wrong.setAlwaysOnTop(true);
        wrong.setVisible(true);
        this.setVisible(false);}
    }
 
    @Override
    public void insertUpdate(DocumentEvent e) {
 
    }
 
    @Override
    public void removeUpdate(DocumentEvent e) {
 
    }
 
    @Override
    public void changedUpdate(DocumentEvent e) {
 
    }
 
    @Override
    public void mouseClicked(MouseEvent e) {
        //点击了注册按钮，调用注册方法
        if(e.getSource()==reghter) {
            System.out.println("点击了注册按钮");
            try {
                write();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
        else System.out.println("有错误");
    }
 
    @Override
    public void mousePressed(MouseEvent e) {
 
    }
 
    @Override
    public void mouseReleased(MouseEvent e) {
 
    }
 
    @Override
    public void mouseEntered(MouseEvent e) {
 
    }
 
    @Override
    public void mouseExited(MouseEvent e) {
 
    }
}
